package com.bishe.pojo.query;

import com.bishe.pojo.Clientinfo;
import com.bishe.utils.SqlQueryUtils;
import lombok.Data;

import java.util.List;

/**
 * 分页返回数据
 * @param <T>
 */
@Data
public class PageResult<T> {
    //总数据量
    private Integer total;

    //每页数据量
    private Integer pageSize;

    //当前页
    private Integer pageNo;

    //数据
    private List<T> result;

    public PageResult(OrderPage orderPage, Integer total, List<T> result){
        this.pageNo = orderPage.getPageNo();
        this.pageSize = orderPage.getPageSize();
        this.total = total;
        this.result = result;
    }

    public PageResult(OrderPage orderPage,SqlQueryUtils<T> sqlQueryUtils, List<T> result){
        //获取sql
        String sql = sqlQueryUtils.getSql();
        //根据分页切割
        String[] s = sql.split("limit");
        //存在分页  去掉参数
        if (s.length >1){
            List<Object> param = sqlQueryUtils.getParam();
            param.remove(param.size()-1);
            param.remove(param.size()-1);
        }
        //设置sql
        sqlQueryUtils.setSql(s[0]);
        //获取总数居
        this.total = sqlQueryUtils.getCount();
        this.pageNo = orderPage.getPageNo();
        this.pageSize = orderPage.getPageSize();
        this.result = result;
    }
}
